package com.horoscope.astrology.zodiac.signs.free.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.MmoConstant
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.ShareViewHolder
import com.horoscope.astrology.zodiac.signs.free.bean.CharacterTextListBean
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager

class CharacterTextListAdapter(
    private val context: Context,
    private val data: ArrayList<CharacterTextListBean>
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            TYPE_CONTENT -> {
                return TextListViewHolder(
                    LayoutInflater.from(context).inflate(
                        R.layout.item_character_text_list,
                        parent,
                        false
                    )
                )
            }
            else -> {
                return ShareViewHolder(
                    LayoutInflater.from(context).inflate(
                        R.layout.item_share,
                        parent,
                        false
                    )
                )
            }
        }

    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is TextListViewHolder -> {
                if (data.size > 0) {
                    holder.title.text = data[position].title
                    holder.content.text = data[position].content
                }
            }
            is ShareViewHolder -> {
                holder.share.setTextColor(ContextCompat.getColor(context, R.color.white))
                holder.facebook.setOnClickListener {
                    ShareManager.shareToFacebook(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
                holder.more.setOnClickListener {
                    ShareManager.sysCallsShare(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
                holder.lineLeft.setBackgroundColor(
                    ContextCompat.getColor(
                        context,
                        R.color.def_gray
                    )
                )
                holder.lineRight.setBackgroundColor(
                    ContextCompat.getColor(
                        context,
                        R.color.def_gray
                    )
                )
            }
        }

    }

    override fun getItemViewType(position: Int): Int {
        return if (position == data.size - 1) {
            TYPE_SHARE
        } else {
            TYPE_CONTENT
        }
    }


    companion object {
        const val TYPE_SHARE = 0x11
        const val TYPE_CONTENT = 0x12
        const val TYPE_TITLE = 0x13
    }

    open inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class TextListViewHolder(itemView: View) : MyViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.tv_title)
        val content: TextView = itemView.findViewById(R.id.tv_content)
    }

}